import {
  ModelOptions
} from '@/dva/types';

import { State } from './types';

import * as effects from './effects';

const initState: State = {
  pageMap: {},
  submitLoading: false
};

export default {
  namespace: 'imageSource',
  state: initState,
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload };
    },
    splitPage(state, { payload }) {
      const { pageMap = {} } = state;
      const { classify, data } = payload;
      return {
        ...state,
        pageMap: {
          ...pageMap,
          [classify]: data
        }
      };
    },
    setLoading(state, { payload }) {
      const { pageMap = {} } = state;
      const { classify, loading } = payload;
      const page = pageMap[classify] || {};
      return {
        ...state,
        pageMap: {
          ...pageMap,
          [classify]: { ...page, loading }
        }
      };
    }
  },
  effects: effects,
} as ModelOptions<State>;
